package com.umeox.watch.moto.poweroff;


import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.TextView;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.dataservice.constants.Constants;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class UploadLocationActivity extends ThemeAppCompatActivity {

    public static final int FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000;
    private static final int POWER_OFF = 0x10;
    public TextView location_tv;
    private int recLen = 0;
    public ScheduledExecutorService executor;
    private LocationReceiver mLocationReceiver = new LocationReceiver();
    private final Handler mHandler = new Handler(msg -> {
        if(msg.what == POWER_OFF){
            Utils.shutdown(UploadLocationActivity.this);
        }
        return false;
    });



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uplocation);

        registerReceiver();

        location_tv = findViewById(R.id.location_tv);

        executor = Executors.newScheduledThreadPool(1);
        executor.scheduleAtFixedRate(
                () -> {
                    recLen++;
                    if (recLen >= 20) {
                        executor.shutdownNow();
                        mHandler.sendEmptyMessage(POWER_OFF);
                    }
                },
                0,
                1000,
                TimeUnit.MILLISECONDS);


    }

    @SuppressLint("WrongConstant")
    private void sendPowerOffBroadCast() {
        Intent intent1 = new Intent();
        intent1.setAction("com.umeox.watch.intent.action.ACTION_POWEROFF");
        intent1.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND | FLAG_RECEIVER_INCLUDE_BACKGROUND);
        sendBroadcast(intent1);
    }

    private void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.ACTION_LOCATION_COMPLETED);
        registerReceiver(mLocationReceiver, intentFilter);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        executor.shutdownNow();
        if (mLocationReceiver != null) {
            unregisterReceiver(mLocationReceiver);
            mLocationReceiver = null;
        }
    }


    private class LocationReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            location_tv.setText(UploadLocationActivity.this.getResources().getString(R.string.upload_complete));
            executor.shutdownNow();
            sendPowerOffBroadCast();
            mHandler.sendEmptyMessageDelayed(POWER_OFF,5000);
        }
    }
}
